package com.cxg.fairy.ec.main.cart;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by CFairy
 */

public class ShopCartDataConverter extends DataConverter {

    private Float mAmount = null;
    private Float mFreight = null;

    public void setAmount(Float amount) {
        this.mAmount = amount;
    }
    public Float getAmount() {
        return mAmount;
    }

    public void setFreight(Float freight) {
        this.mFreight = freight;
    }
    public Float getFreight() {
        return mFreight;
    }

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        final Float amount = JSON.parseObject(getJsonData()).getFloat("amount");
        final Float freight = JSON.parseObject(getJsonData()).getFloat("freight");
        setAmount(amount);
        setFreight(freight);
        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final String thumb = data.getString("thumb");
            final String desc = data.getString("desc");
            final String title = data.getString("title");
            final String itemType = data.getString("itemType");
            final boolean promotionStatus = data.getBoolean("promotionStatus")==null?false:data.getBoolean("promotionStatus");

            final boolean isSelected = data.getBoolean("isSelected")==null?false:data.getBoolean("isSelected");

            final double promotionPrice = data.getDouble("promotionPrice")==null?0:data.getDouble("promotionPrice");
            final String price = data.getString("price");
            final int id = data.getInteger("skuId")==null?0:data.getInteger("skuId");
            final int cartCount = data.getInteger("cartQuantity")==null?0:data.getInteger("cartQuantity");
            final int promotionLimitQuantity = data.getInteger("promotionLimitQuantity")==null?0:data.getInteger("promotionLimitQuantity");
            final double defaultPrice = data.getDouble("defaultPrice")==null?0:data.getDouble("defaultPrice");
            final int spanSize = data.getInteger("spanSize");
            final String cartDecotext = data.getString("text");
            final String cartGoodsName = data.getString("cartGoodsName");
            final boolean status = data.getBoolean("status")==null?false:true;
            final String unit = data.getString("unit");

//            LatteLogger.e("promotionPrice=",promotionPrice+"");
            int type = 0;
            if("CARTDECORATION".equalsIgnoreCase(itemType)){
                type = ShopCartItemType.SHOP_CART_MIGHT_LIKE_TAG;
            } else if ("CARTSUGGESTGOODS".equalsIgnoreCase(itemType)) {
                type = ShopCartItemType.SHOP_CART_MIGHT_LIKE_GOODS;
            } else if ("CARTITEMS".equalsIgnoreCase(itemType)) {
                type = ShopCartItemType.SHOP_CART_ITEM;
            }

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.IMAGE_URL, thumb)
                    .setField(ShopCartItemFields.TITLE, title)
                    .setField(ShopCartItemFields.DESC, desc)
                    .setField(ShopCartItemFields.COUNT, cartCount)
                    .setField(ShopCartItemFields.PRICE, price)
                    .setField(ShopCartItemFields.DEFAULT_PRICE, defaultPrice)
                    .setField(ShopCartItemFields.PROMOTION_PRICE, promotionPrice)
                    .setField(ShopCartItemFields.PROMOTION_STATUS,promotionStatus)
                    .setField(ShopCartItemFields.PROMOTION_LIMIT,promotionLimitQuantity)
                    .setField(ShopCartItemFields.GOODS_NAME, cartGoodsName)
                    .setField(ShopCartItemFields.TEXT, cartDecotext)
                    .setField(ShopCartItemFields.STATUS, status)
                    .setField(ShopCartItemFields.UNIT, unit)
                    .setField(ShopCartItemFields.IS_SELECTED, isSelected)
                    .setField(ShopCartItemFields.POSITION, i)
                    .setField(ShopCartItemFields.SPAN_SIZE,spanSize)
                    .build();

            dataList.add(entity);
        }

        return dataList;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }
}
